package project.youkumenu.customview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.util.AttributeSet;

import java.util.List;

import project.youkumenu.R;

/**
 * Created by lgs on 2018/3/7.
 */

public class SKeyboardView extends KeyboardView {

    private Context context;
    private Rect rect;
    private Paint mPaint;

    private int keyboardType = -1;

    public SKeyboardView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SKeyboardView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
    }

    /**
     * 初始化画笔等
     */
    private void init() {
        rect = new Rect();
        mPaint = new Paint();
        mPaint.setTextSize(70);
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.BLACK);
        mPaint.setTextAlign(Paint.Align.CENTER);
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (null == getKeyboard()) {
            return;
        }
        List<Keyboard.Key> keys = getKeyboard().getKeys();

        if (keyboardType == 0) {
            drawKeyboardNumber(keys, canvas);
        }
    }

    /**
     * 绘制数字键盘
     *
     * @param keys
     * @param canvas
     */
    private void drawKeyboardNumber(List<Keyboard.Key> keys, Canvas canvas) {
        for (Keyboard.Key key : keys) {
            if (key.codes[0] == -5) {   //删除键
                drawKeyBackground(R.drawable.img_edit_clear, canvas, key);
            }
        }
    }

    private void drawKeyboardEnglish(List<Keyboard.Key> keys, Canvas canvas) {
        for (Keyboard.Key key : keys) {
            if (key.codes[0] == -5) { //删除键
                drawKeyBackground(R.drawable.img_edit_clear, canvas, key);
            }
            if (key.codes[0] == -1) { //大小写切换
                drawKeyBackground(R.drawable.img_edit_clear, canvas, key);
            }
            if (key.codes[0] == 32) { //space

            }
            if (key.codes[0] == -4) { //完成

            }
        }
    }

    /**
     * 设置当前键盘标识 0：数字键盘；1：英文键盘
     * @param keyboardType
     */
    public void setCurrentKeyboard(int keyboardType){
        this.keyboardType = keyboardType;
        invalidate();
    }

    /**
     * 绘制键盘key的背景
     *
     * @param drawableId 将要绘制上去的图标
     * @param canvas
     * @param key        需要绘制的键
     */
    private void drawKeyBackground(int drawableId, Canvas canvas, Keyboard.Key key) {
//        Drawable npd = ResUtil
    }
}
